package com.ATMgui;

import java.sql.*;
import java.util.ArrayList;

public class DBUtil {
    /*
创建数据库连接
 */
    public Connection getConnection() {
        Connection con = null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/j189?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "997424586qwE!");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    public void close(Connection con, Statement statement) {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public void close(Connection con, Statement statement, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 根据用户名和密码查询用户对象
     *
     * @param username
     * @param password
     * @return
     */
    public Users getByUsernameAndPwd(String username, String password) {
        Connection con = getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        Users users = null;
        String sql = "select * from t_users where u_name= ? and u_pwd= ?";
        try {
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setObject(1, username);
            preparedStatement.setObject(2, password);
            rs = preparedStatement.executeQuery();
            if (rs.next()) {
                users = new Users();
                users.setId(rs.getInt("u_id"));
                users.setUserName(rs.getString("u_name"));
                users.setPassWord(rs.getString("u_pwd"));
                users.setBlance(rs.getDouble("u_blance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, preparedStatement, rs);
        }
        return users;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public Users getById(int id) {
        Connection con = getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        Users users = null;
        String sql = "select * from t_users where u_id=?";
        try {
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setObject(1, id);
            rs = preparedStatement.executeQuery();
            if (rs.next()) {
                users = new Users();
                users.setId(rs.getInt("u_id"));
                users.setUserName(rs.getString("u_name"));
                users.setPassWord(rs.getString("u_pwd"));
                users.setBlance(rs.getDouble("u_blance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, preparedStatement, rs);
        }
        return users;
    }

    /**
     * 根据用户名查询用户对象
     *
     * @param username
     * @return
     */
    public Users getByUsername(String username) {
        Connection con = getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        Users users = null;
        String sql = "select * from t_users where u_name=?";
        try {
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setObject(1, username);
            rs = preparedStatement.executeQuery();
            if (rs.next()) {
                users = new Users();
                users.setId(rs.getInt("u_id"));
                users.setUserName(rs.getString("u_name"));
                users.setPassWord(rs.getString("u_pwd"));
                users.setBlance(rs.getDouble("u_blance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, preparedStatement, rs);
        }
        return users;
    }


    public void update(Users loginUser,Users otherUser) {
        // 1. 创建连接
        Connection con = getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "update t_users set u_pwd=?,u_blance=? where u_id=?";
        try {
            // 2. 获取执行SQL语句对象
            con.setAutoCommit(false);
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setObject(1, loginUser.getPassWord());
            preparedStatement.setObject(2, loginUser.getBlance());
            preparedStatement.setObject(3, loginUser.getId());
            // 3. 执行SQL语句
            preparedStatement.executeUpdate();
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setObject(1, otherUser.getPassWord());
            preparedStatement.setObject(2, otherUser.getBlance());
            preparedStatement.setObject(3, otherUser.getId());
            // 3. 执行SQL语句
            preparedStatement.executeUpdate();
            con.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            // 4. 关闭连接
            close(con, preparedStatement);
        }
    }

    public void update(Users Users) {
        // 1. 创建连接
        Connection con = getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "update t_users set u_pwd=?,u_blance=? where u_id=?";
        try {
            // 2. 获取执行SQL语句对象
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setObject(1, Users.getPassWord());
            preparedStatement.setObject(2, Users.getBlance());
            preparedStatement.setObject(3, Users.getId());
            // 3. 执行SQL语句
            preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4. 关闭连接
            close(con, preparedStatement);
        }
    }

}
